﻿using C1.Phone.FlexGrid;
using C1.Silverlight.Resources;
using SmartMobile.Data;
using SMARTMobile.Models.Local;
using SMARTMobile.WinPhone;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SMARTMobile.Common;
using SMARTMobile.Services.SMARTMobileServiceReference;
using SMARTMobile.Models;
using SMARTMobile.Models.UI;

namespace SMARTMobile.ViewModels
{
    public class OutetPGInformationViewModel : ViewModelBase
    {
        #region Fields

        private SmartMobileDBContext _smartMobileContext;
        private ObservableCollection<OutletPGInformationExtend> _outetPGInformationObservable;  
  
        #endregion

        public OutetPGInformationViewModel()
        {
            this.CreateConnection();
            this.GetOutletPGInformation();

            this.SaveCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteSaveCommand
            };

            this.ExitCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteExitCommand
            };
        }

        #region Properties

        public ObservableCollection<OutletPGInformationExtend> OutetPGInformationObservable
        {
            get { return this._outetPGInformationObservable; }
            set
            {
                this._outetPGInformationObservable = value;
                base.RaisePropertyChanged("OutetPGInformationObservable");
            }
        }

        #endregion

        #region Command
        public ICommandRoute ExitCommand
        {
            get;

            private set;
        }

        public ICommandRoute SaveCommand
        {
            get;

            private set;
        }
        #endregion

        #region Command Action Handlers
        private void ExecuteExitCommand(object commandParams)
        {
            this.ExitCommand.RaiseCanExecuteChanged(true);
            base.PageNavigation.Navigate("InputData");
        }

        private void ExecuteSaveCommand(object commandParams)
        {
            if (GlobalInformation.SelectedTCARCUS != null)
            {
                this._smartMobileContext.OutetPGInformation.DeleteAllOnSubmit(this._smartMobileContext.OutetPGInformation.Where(x => x.Customerkey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList());       

                this._smartMobileContext.SubmitChanges();
            }

            List<OutetPGInformation> outletPGList = new List<OutetPGInformation>();
            foreach (var pgInformation in this.OutetPGInformationObservable)
            {
                OutetPGInformation outletPG = new OutetPGInformation
                {
                    ID_PK = pgInformation.ID_PK,
                    Customerkey = pgInformation.Customerkey,
                    PhoneVendorCode = pgInformation.PhoneVendorCode,
                    Qty = pgInformation.Qty
                };

                outletPGList.Add(outletPG);
            }
            this._smartMobileContext.OutetPGInformation.InsertAllOnSubmit(outletPGList);
            this._smartMobileContext.SubmitChanges();

            this.IsBusy = false;

            this.ShowMessage("Lưu thành công");
            this.SaveCommand.RaiseCanExecuteChanged(true);
        }
        #endregion

        #region Method

        private void GetOutletPGInformation()
        {
            List<PhoneVendor> phoneVendors = this._smartMobileContext.PhoneVendor.ToList();
            if (phoneVendors.Count == 0)
            {
                return;
            }

            this.OutetPGInformationObservable = new ObservableCollection<OutletPGInformationExtend>();
            foreach (var item in phoneVendors)
            {
                OutletPGInformationExtend outletPG = new OutletPGInformationExtend
                {
                    ID_PK = Guid.NewGuid(),
                    Customerkey = GlobalInformation.SelectedTCARCUS.Customerkey,
                    PhoneVendorCode = item.PhoneVendorCode,
                    PhoneVendorName = item.PhoneVendorName,
                    Qty = 0
                };
                this.OutetPGInformationObservable.Add(outletPG);
            }

            if (GlobalInformation.SelectedTCARCUS != null)
            {
                List<OutetPGInformation> outletPGInformations = this._smartMobileContext.OutetPGInformation.Where(p => p.Customerkey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList();
                                
                if (outletPGInformations.Count > 0)
                {
                    if (outletPGInformations.Count == this.OutetPGInformationObservable.Count)
                    {
                        this.OutetPGInformationObservable = new ObservableCollection<OutletPGInformationExtend>();
                        foreach (var item in outletPGInformations)
                        {
                            OutletPGInformationExtend outletPG = new OutletPGInformationExtend
                            {
                                ID_PK = item.ID_PK,
                                Customerkey = item.Customerkey,
                                PhoneVendorCode = item.PhoneVendorCode,
                                PhoneVendorName = this.GetPhoneVendorName(item.PhoneVendorCode),
                                Qty = item.Qty
                            };
                            this.OutetPGInformationObservable.Add(outletPG);
                        }
                    }
                    else
                    {
                        foreach (var item in outletPGInformations)
                        {
                            OutletPGInformationExtend selectedItem = this.OutetPGInformationObservable.FirstOrDefault(o => (o.Customerkey == item.Customerkey && o.PhoneVendorCode == item.PhoneVendorCode));
                            if (selectedItem != null)
                            {
                                selectedItem.ID_PK = item.ID_PK;
                                selectedItem.Customerkey = item.Customerkey;
                                selectedItem.PhoneVendorCode = item.PhoneVendorCode;
                                selectedItem.PhoneVendorName = this.GetPhoneVendorName(item.PhoneVendorCode);
                                selectedItem.Qty = item.Qty;
                            }                            
                        }

                        this._smartMobileContext.OutetPGInformation.DeleteAllOnSubmit(this._smartMobileContext.OutetPGInformation);
                        this._smartMobileContext.SubmitChanges();

                        List<OutetPGInformation> outletPGList = new List<OutetPGInformation>();
                        foreach (var pgInformation in this.OutetPGInformationObservable)
                        {
                            OutetPGInformation outletPG = new OutetPGInformation
                            {
                                ID_PK = pgInformation.ID_PK,
                                Customerkey = pgInformation.Customerkey,
                                PhoneVendorCode = pgInformation.PhoneVendorCode,
                                Qty = pgInformation.Qty
                            };

                            outletPGList.Add(outletPG);
                        }
                        this._smartMobileContext.OutetPGInformation.InsertAllOnSubmit(outletPGList);
                        this._smartMobileContext.SubmitChanges();
                    }
                }
                else
                {
                    List<OutetPGInformation> outletPGList = new List<OutetPGInformation>();
                    foreach (var pgInformation in this.OutetPGInformationObservable)
                    {
                        OutetPGInformation outletPG = new OutetPGInformation
                        {
                            ID_PK = pgInformation.ID_PK,
                            Customerkey = pgInformation.Customerkey,
                            PhoneVendorCode = pgInformation.PhoneVendorCode,
                            Qty = pgInformation.Qty
                        };

                        outletPGList.Add(outletPG);
                    }
                    this._smartMobileContext.OutetPGInformation.InsertAllOnSubmit(outletPGList);
                    this._smartMobileContext.SubmitChanges();
                }
            }
        }

        private string GetPhoneVendorName(string phonevendorCode)
        {
            return this._smartMobileContext.PhoneVendor.Where(p => p.PhoneVendorCode == phonevendorCode).Select(p => p.PhoneVendorName).FirstOrDefault();
        }

        private void CreateConnection()
        {
            this._smartMobileContext = new SmartMobileDBContext(SmartMobileDBContext.ConnectionString);
            this._smartMobileContext.CreateIfNotExists();
            this._smartMobileContext.LogDebug = true;
        }
        #endregion
    }
}
